package com.shijie.newvideo.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import com.shijie.newvideo.R;


public class AutoResizeHeightHelper {
    /**
     * 宽高比，宽度/高度
     */
    private float mWhRate;

    public AutoResizeHeightHelper(Context context, AttributeSet attrs, int defStyleAttr) {
        if (attrs != null) {
            TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AutoResizeHeightImageView, defStyleAttr, 0);
            try {
                float whRate = a.getFloat(R.styleable.AutoResizeHeightImageView_wh_rate, 0);
                if (whRate != 0) {
                    //设置宽高比
                    mWhRate = whRate;
                } else {
                    //没有设置宽高比，读取宽度
                    float width = a.getFloat(R.styleable.AutoResizeHeightImageView_wh_width, 0);
                    if (width != 0) {
                        //不为0才读取高度，else里面没处理，默认为0
                        //默认为1，这样可以只设置宽度即可
                        float height = a.getFloat(R.styleable.AutoResizeHeightImageView_wh_height, 1.0f);
                        mWhRate = width / height;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            a.recycle();
        }
    }

    public float getWhRate() {
        return mWhRate;
    }

    public void setWhRate(float whRate) {
        mWhRate = whRate;
    }
}
